package com.entrogy.controller;

import com.Sinewecology.config.ConfigParams;
import com.entrogy.bean.*;
import com.entrogy.service.*;
import com.entrogy.timing.ChangePersonTimeThread;
import com.entrogy.timing.TimingTaskService;
import com.entrogy.utils.*;
import com.entrogy.utils.constants.ErrorEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xlr
 * @data 2019-05-16
 */
@RestController
@Api(description = "人员注册接口,注册之后就可以通行闸机")
@RequestMapping(value = "/registor")
public class RegistorController {

    @Value("${image.root.path}")
    private String rootPath;

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public Map<String,Object> registorMethod(MultipartFile file, String name){
        try {
            String storeDirectory = "C:/registorImge";
            String fileName = "";
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            bufferedImage = ZoomImageUtil.zoomImage(bufferedImage);
            BDFaceDetect bdFaceDetect = new BDFaceDetect();
            org.json.JSONObject ret = bdFaceDetect.getAvailableBase64(bufferedImage);
            if (ret.getInt("result") == 1) {
                byte[] newImgByte = bdFaceDetect.getBytes_hxy(ret.getString("picbase64"));
                File path = new File(storeDirectory);
                if (!path.exists()) {
                    path.mkdirs();
                }
                fileName = IdGenetor.genUUIDFileName() + "_" + file.getOriginalFilename();
                FileOutputStream out = new FileOutputStream(new File(path, fileName));
                out.write(newImgByte);
                out.flush();
                out.close();
            }
            Map<String,Object> map = new HashMap<>();
            map.put("imgUrl", fileName);
            map.put("code", Integer.valueOf(ret.getInt("result")));// 返回状态 0--重新上传 2--重新上传 1--上传成功
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ApiReturnUtil.error( "亲,照片有问题哦.");
    }
}
